package com.boilerCloud.dao.log;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.boilerCloud.dao.BaseDao;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.device.ModelInfo;
import com.boilerCloud.dto.log.OperatorLog;
import com.boilerCloud.util.StringUtil;

@Repository
public class LogDao extends BaseDao {

	public void qryList(OperatorLog model, HzlqswPage<OperatorLog> page)throws Exception {
		
		String countsql = "select count(*) from operator_log a "
				+ " where 1=1 ";
			
		String listSql = "select a.* "
					+ " from operator_log a "
					
					+ " where 1=1 ";
		List<Object> params= new ArrayList<Object>();
		if(!StringUtil.isEmpty(model.getName())){
			countsql += " and a.name like ?";
			listSql += " and a.name like ?";
			params.add("%" + model.getName() +"%");
		}
		
		if(!StringUtil.isEmpty(model.getUserAccount())){
			countsql += " and a.user_account like ?";
			listSql += " and a.user_account like ?";
			params.add("%" + model.getUserAccount() +"%");
		}
		
		if(!StringUtil.isEmpty(model.getType())){
			countsql += " and a.type like ?";
			listSql += " and a.type like ?";
			params.add("%" + model.getType() +"%");
		}
			
		listSql += " order by id desc ";
		this.queryListByPage(countsql, params, listSql, params, page, OperatorLog.class);
	}

	public void addOperatorLog(OperatorLog model)throws Exception {
		String sql = "insert into operator_log(name,content,data,create_time,type,user_name,"
				+ "user_type,user_account,ip_address,uri) values(?,?,?,now(),?,?,?,?,?,?)";
		List<Object> params = new ArrayList<>();
		params.add(model.getName());
		params.add(model.getContent());
		params.add(model.getData());
		params.add(model.getType());
		params.add(model.getUserName());
		params.add(model.getUserType());
		params.add(model.getUserAccount());
		params.add(model.getIpAddress());
		params.add(model.getUri());
		//params.add(model.getName());
		this.jdbcTemplate.update(sql,params.toArray());
	}

}
